package GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Window.Type;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JScrollPane;
import java.awt.Component;
import java.sql.ResultSet;

import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JButton;

import Repositorio.Rep_Aluno;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ListaAluno extends JFrame {

	private JPanel contentPane;
	private JTextField txtPesquisa;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ListaAluno frame = new ListaAluno();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	
	
	private JTable tabela;
	private DefaultTableModel modelo;
	Rep_Aluno rep = new Rep_Aluno();
	
	
	
	
	private void CriaTabela(){
		
		tabela = new JTable(modelo);
		modelo.addColumn("ID");
		modelo.addColumn("Nome");
		modelo.addColumn("CPF");
		modelo.addColumn("email");
		tabela.getColumnModel().getColumn(0).setPreferredWidth(20);
		tabela.getColumnModel().getColumn(1).setPreferredWidth(120);
		tabela.getColumnModel().getColumn(2).setPreferredWidth(100);
		tabela.getColumnModel().getColumn(3).setPreferredWidth(50);
		
		
		
	}
	
	private void PesquisaAluno(){
		
		CriaTabela();
		ResultSet rs = rep.ListaAluno(txtPesquisa.getText());
		try 
		{
			rs.first();
			while (rs.next());
			{
				modelo.addRow(new Object [] {rs.getObject("cod_aluno"), rs.getObject("nome"),
							 rs.getObject("email")});
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	
	
	public ListaAluno() {
		
		JOptionPane.setRootFrame(this);
		setResizable(false);
		setAlwaysOnTop(true);
		setResizable(false);
		setType(Type.NORMAL);
				
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 533, 480);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(0, 0, 517, 33);
		contentPane.add(panel);
		panel.setLayout(null);
		
		txtPesquisa = new JTextField();
		txtPesquisa.setBounds(117, 6, 307, 20);
		txtPesquisa.setColumns(40);
		panel.add(txtPesquisa);
		
		JButton btnSearch = new JButton("Procurar");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				PesquisaAluno();
			}
		});
		btnSearch.setBounds(434, 5, 73, 23);
		panel.add(btnSearch);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(2, 44, 515, 396);
		contentPane.add(scrollPane);
	}
	
}